﻿namespace QLPhongKham.BaseClass.Chung
{
    using ConfigDatabase.Class;
    using Npgsql;
    using NpgsqlTypes;
    using System;
    using System.Data;

    public class dtoDSThongSo
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private decimal _deid;
        private int _istt;
        private string _sgiatri1;
        private string _sgiatri2;
        private string _sngaycn;
        private string _sSchema;
        private string _sten;

        public dtoDSThongSo()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDSThongSo(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDSThongSo.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand("delete from " + this._sSchema + "." + tableName + " where 1=1  ", connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDSThongSo.cs");
                return false;
            }
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoDSThongSo.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) as idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = int.Parse(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoDSThongSo.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDSThongSo.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnSTT, ",", columnTEN, ",", columnGIATRI1, ",", columnGIATRI2, ") values(", this._deid, 
                    ",", this._istt, ",:", columnTEN, ",:", columnGIATRI1, ",:", columnGIATRI2, ")"
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTEN, NpgsqlDbType.Text, 200).Value = this._sten;
                command.Parameters.Add(columnGIATRI1, NpgsqlDbType.Text, 200).Value = this._sgiatri1;
                command.Parameters.Add(columnGIATRI2, NpgsqlDbType.Text, 200).Value = this._sgiatri2;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDSThongSo.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0M;
            this._istt = 0;
            this._sten = "";
            this._sgiatri1 = "";
            this._sgiatri2 = "";
            this._sngaycn = "";
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoDSThongSo.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(string.Concat(new object[] { 
                    " select  ", columnID, ",", columnSTT, ",", columnTEN, ",", columnGIATRI1, ",", columnGIATRI2, ",to_char(", columnNGAYCN, ",'dd/mm/yyyy') as ", columnNGAYCN, " from ", this._sSchema, 
                    ".", tableName, " where ", columnID, "=", this._deid, " and ", columnSTT, "=", this._istt, (this._sgiatri1 != "") ? (" and " + columnGIATRI1 + "='" + this._sgiatri1 + "'") : "", (this._sgiatri2 != "") ? (" and " + columnGIATRI2 + "='" + this._sgiatri2 + "'") : ""
                 }), connect).Fill(dataTable);
                if (dataTable.Rows.Count != 0)
                {
                    this._deid = decimal.Parse(dataTable.Rows[0][columnID].ToString());
                    this._istt = int.Parse(dataTable.Rows[0][columnSTT].ToString());
                    this._sten = dataTable.Rows[0][columnTEN].ToString();
                    this._sgiatri1 = dataTable.Rows[0][columnGIATRI1].ToString();
                    this._sgiatri2 = dataTable.Rows[0][columnGIATRI2].ToString();
                    this._sngaycn = dataTable.Rows[0][columnNGAYCN].ToString();
                }
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoDSThongSo.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoDSThongSo.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnSTT + "," + columnTEN + "," + columnGIATRI1 + "," + columnGIATRI2 + ",to_char(" + columnNGAYCN + ",'dd/mm/yyyy') as " + columnNGAYCN + " from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoDSThongSo.cs");
                return null;
            }
        }

        public bool TLQuyenSuDung(string quyensudung, int idChuongTrinh)
        {
            bool flag;
            this._connect = new NpgsqlConnection(this._condata.StringConnect);
            this._connect.Open();
            try
            {
                flag = this.TLQuyenSuDung(this._connect, quyensudung, idChuongTrinh);
            }
            catch
            {
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool TLQuyenSuDung(NpgsqlConnection _connect, string ten, int idChuongTrinh)
        {
            this.KhoiTao();
            this._deid = 4M;
            this._istt = idChuongTrinh;
            this.Load(_connect);
            this._sgiatri1 = ten;
            if (!this.Update(_connect) && !this.Insert(_connect))
            {
                return false;
            }
            return true;
        }

        public string TVQuyenSuDung(int idChuongTrinh)
        {
            string str;
            this._connect = new NpgsqlConnection(this._condata.StringConnect);
            this._connect.Open();
            try
            {
                str = this.TVQuyenSuDung(this._connect, idChuongTrinh);
            }
            catch
            {
                str = "";
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return str;
        }

        public string TVQuyenSuDung(NpgsqlConnection _connect, int idChuongTrinh)
        {
            this.KhoiTao();
            this._deid = 4M;
            this._istt = idChuongTrinh;
            this.Load(_connect);
            return this._sgiatri1;
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDSThongSo.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { 
                    "update ", this._sSchema, ".", tableName, " set ", columnTEN, " = :", columnTEN, ",", columnGIATRI1, " = :", columnGIATRI1, ",", columnGIATRI2, " = :", columnGIATRI2, 
                    ",", columnNGAYCN, " =now()  where ", columnID, " = ", this._deid, " and ", columnSTT, " = ", this._istt, (this._sgiatri2 != "") ? (" and " + columnGIATRI2 + "='" + this._sgiatri2 + "'") : ""
                 }), connect) {
                    CommandType = CommandType.Text
                };
                command.Parameters.Add(columnTEN, NpgsqlDbType.Text, 200).Value = this._sten;
                command.Parameters.Add(columnGIATRI1, NpgsqlDbType.Text, 200).Value = this._sgiatri1;
                command.Parameters.Add(columnGIATRI2, NpgsqlDbType.Text, 200).Value = this._sgiatri2;
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDSThongSo.cs");
                return false;
            }
        }

        public static string columnGIATRI1
        {
            get
            {
                return "giatri1";
            }
        }

        public static string columnGIATRI2
        {
            get
            {
                return "giatri2";
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnNGAYCN
        {
            get
            {
                return "ngaycn";
            }
        }

        public static string columnSTT
        {
            get
            {
                return "stt";
            }
        }

        public static string columnTEN
        {
            get
            {
                return "ten";
            }
        }

        public string GIATRI1
        {
            get
            {
                return this._sgiatri1;
            }
            set
            {
                this._sgiatri1 = value;
            }
        }

        public string GIATRI2
        {
            get
            {
                return this._sgiatri2;
            }
            set
            {
                this._sgiatri2 = value;
            }
        }

        public decimal ID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public string NGAYCN
        {
            get
            {
                return this._sngaycn;
            }
            set
            {
                this._sngaycn = value;
            }
        }

        public int STT
        {
            get
            {
                return this._istt;
            }
            set
            {
                this._istt = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "dsthongso";
            }
        }

        public string TEN
        {
            get
            {
                return this._sten;
            }
            set
            {
                this._sten = value;
            }
        }
    }
}

